package org.afs.gescol.core.dao.implementations;

import java.util.List;

import org.afs.gescol.core.dao.interfaces.SchoolClassDao;
import org.afs.gescol.core.dto.DataCriteria;
import org.afs.gescol.core.model.data.SchoolClass;
import org.afs.gescol.core.model.data.SubjectAssignment;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.scub.foundation.framework.core.dao.implementations.HibernateDaoBaseImplementation;

/**
 * SchoolClassDao implementation.
 * @autor Kobla GBENYO (kobla.gbenyo@gmail.com)
 */
public class SchoolClassDaoImpl extends HibernateDaoBaseImplementation implements SchoolClassDao {

    @Override
    public void createSchoolClass(SchoolClass schoolClass) {
        save(schoolClass);
    }

    @Override
    public SchoolClass getSchoolClassById(Long id) {
        return load(id, SchoolClass.class);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<SchoolClass> getSchoolClassByLabel(String label) {
        final Criteria criteria = createCriteria(SchoolClass.class);
        if (label != null && !"".equals(label)) {
            criteria.add(Restrictions.or(
                Restrictions.ilike("shortLabel", label, MatchMode.ANYWHERE),
                Restrictions.ilike("longLabel", label, MatchMode.ANYWHERE)
            ));
        }
        criteria.addOrder(Order.asc("shortLabel"));
        return criteria.list();
    }

    @Override
    public void deleteSchoolClass(SchoolClass schoolClass) {
        delete(schoolClass);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<SchoolClass> getSchoolClassByCriteria(DataCriteria criteria) {
        final Criteria searchCriteria = createCriteria(SchoolClass.class);
        if (criteria.getSeriesId() != null) {
            searchCriteria.add(Restrictions.eq("classSeries.id", criteria.getSeriesId()));
        }
        if (criteria.getLevelId() != null) {
            searchCriteria.add(Restrictions.eq("classLevel.id", criteria.getLevelId()));
        }
        if (criteria.getCycleId() != null) {
            searchCriteria.add(Restrictions.eq("classCycle.id", criteria.getCycleId()));
        }
        return searchCriteria.list();
    }

}
